package com.minglead.crawler.schedule;

import com.minglead.crawler.base.ActionResult;
import com.minglead.crawler.constant.YqCrawlerConstant;
import com.minglead.crawler.exception.BizCommonException;
import com.minglead.crawler.feign.PriceFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @description: 更新媒体全局关键词定时任务
 * @author: tzf
 * @date: 2024-1-8
 **/
@Configuration
@EnableScheduling
public class YqCrawlerUpdateGlobalKeywordTask extends ScheduledTaskCommon {

    @Autowired
    private PriceFeignClient priceFeignClient;

    @Override
    public String getCron() {
        return "0 */2 * * * ?";
    }//每两分钟执行一次


    @Override
    @Transactional
    public void execute() {
        ActionResult actionResult = priceFeignClient.getGlobalKeyword();
        if(actionResult.getCode() != 200){
            throw new BizCommonException("获取全局关键词出错");
        }
        String fileContent = actionResult.getMsg();
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(YqCrawlerConstant.CRAWLER_GLOBAL_KEYWORD, false))) {
            writer.write(fileContent);
            System.out.println("更新媒体全局关键词定时任务结束，全局关键词文件更新成功。");
        } catch (IOException e) {
            System.err.println("全局关键词文件更新失败: " + e.getMessage());
        }
    }
}
